<template>
    <div id="monthWorkList">
      <el-dialog title="周报查看" :visible.sync="dialogFormVisibleView">
        <el-form> 
          <el-form-item class="mb0" label="日期：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ viewDate }}</p>
            </el-col>
          </el-form-item>
          <el-form-item label="项目组：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ viewGroup }}</p>
            </el-col>
          </el-form-item>
          <el-form-item label="上周总结：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ viewLast }}</p>
            </el-col>
          </el-form-item>
          <el-form-item label="本周计划：" :label-width="formLabelWidth">
            <el-col :span="24">
              <p class="text-left">{{ viewPlan }}</p>
            </el-col>
          </el-form-item>
        </el-form-item>
        </el-form>
      </el-dialog>
      <el-dialog title="周报修改" :visible.sync="dialogFormVisible">
        <el-form :label-position="labelPosition" :rules="rules" ref="form" :model="form">
          <el-form-item label="选择日期" prop="weekly_date" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-date-picker type="date" :disabled="codeBtype" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="form.weekly_date" style="width: 100%;"></el-date-picker>
            </el-col>
          </el-form-item>
          <el-form-item label="项目组" prop="group" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-select style="width: 100%;" :disabled="codeBtype" v-model="form.group" clearable placeholder="请选择项目组">
                <el-option
                  v-for="item in groupOptionsOnly"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
          <el-form-item label="上周总结" prop="summary_of_last_week" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input type="textarea" :disabled="codeBtype" :rows="5" v-model="form.summary_of_last_week"></el-input>
            </el-col>
          </el-form-item>
          <el-form-item label="本周计划" prop="this_week_plan" :label-width="formLabelWidth">
            <el-col :span="12">
              <el-input type="textarea" :disabled="codeBtype" :rows="5" v-model="form.this_week_plan"></el-input>
            </el-col>
          </el-form-item>
        </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="formSubmit('form')">确 定</el-button>
          <!-- <el-button @click="resetForm('form')">重置</el-button> -->
        </div>
      </el-dialog>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="姓名">
          <el-select v-model="formInline.user" clearable placeholder="请选择姓名" @change="getTableList">
            <el-option
              v-for="item in nameOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="项目组">
          <el-select v-model="formInline.group" clearable placeholder="请选择项目组" @change="getTableList">
            <el-option
              v-for="item in groupOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="选择日期">
          <el-date-picker @change="getTableList" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" v-model="formInline.date" style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="weekly_date"
            label="日期"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="user_name"
            label="姓名"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="group_name"
            label="项目组"
            align="center"
            width="180">
          </el-table-column>
          <el-table-column
            prop="summary_of_last_week"
            label="上周总结"
            align="center">
          </el-table-column>
          <el-table-column
            prop="this_week_plan"
            label="本周计划"
            align="center">
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="300">
            <template slot-scope="scope">
              <el-button v-if="showLook" @click="handleViewClick(scope.row)" type="text" size="small">查看</el-button>
              <el-button v-if="showModify" @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
              <el-button v-if="showDel" @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[15, 25, 50]"
            :page-size="perPage"
            layout="total, sizes,prev, pager, next,jumper"
            :total="total">
          </el-pagination>
        </div>
        <el-input v-model="rowId" type="hidden"></el-input>
    </div>
</template>
<script>
export default {
  name: 'monthWorkList',
  props:['valueGroup'],
  data () {
    return {
      formInline: {
        user: '',
        //group:'',
        date: ''
      },
      form: {
        group: '',
        weekly_date: '',
        summary_of_last_week: '',
        this_week_plan: ''
      },
      rules: {
        weekly_date: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
        group: [
          { required: true, message: '请选择项目组', trigger: 'change' }
        ],
        summary_of_last_week: [
          { required: true, message: '请输入上周总结', trigger: 'blur' }
        ],
        this_week_plan: [
          { required: true, message: '请输入本周计划', trigger: 'blur' }
        ]
      },
      showLook:true,
      showModify:true,
      showDel:true,
      viewDate:'',
      viewGroup:'',
      viewLast:'',
      viewPlan:'',
      codeBtype:false,
      labelPosition: 'right',
      rowId:'',
      formLabelWidth: '260px',
      dialogFormVisible:false,
      dialogFormVisibleView:false,
      nameOptions:[],
      //groupOptions:[],
      groupOptionsOnly:[],
      currentPage: 1,
      perPage: 15,
      //总页数
      total: null,
      tableData: []
    };
  },
  created() {
    this.getTableList();
    this.getNameList();
    this.rootSelect();
    //this.getGroupList();
  },
  beforeDestroy() {},
  computed: {},
  mounted() {},
  methods: {
    onSubmit() {
      console.log('submit!');
      var self = this;      
      self.getTableList();
    },
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`);
      var self = this;
      self.perPage = val;
      self.getTableList();
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`);
      var self = this;
      self.currentPage = val;
      self.getTableList();
    },
    getTableList() {
      var self = this;
      console.log(typeof self.formInline.date)
      if(self.formInline.date == null){
        self.formInline.date ='';
      }
      self.getRequest("/weekly/queryWeekly?page_no=" +self.currentPage+'&&page_size='+self.perPage
      +'&&user_id='+self.formInline.user+'&&weekly_date='+self.formInline.date+'&&group_id='+this.defines.globalValueGroup
      , {}).then(
        res => {      
          console.log(res.data)     
          self.tableData = res.data.data;
          self.total = res.data.recordsTotal;
          self.perPage = res.data.length;
        }
      );
    },
    getNameList() {
      var self = this;
      //console.log(self.currentPage)
      self.getRequest("/base/common/queryUserNameByGroupId?group_id="+this.defines.globalValueGroup, {}).then(
        res => {      
          console.log(res.resultData)     
          var obj = res.data.resultData;
            var arr = [];
            for(var i in obj) {
              var row = {};
              row.label = obj[i].name;
              row.value = obj[i].id;
              arr.push(row);
            }
            //console.log(arr)
            self.nameOptions = arr;
        }
      );
    },
    // getGroupList() {
    //   var self = this;
    //   //console.log(self.currentPage)
    //   self.getRequest("/base/common/queryProjectName?flug="+2, {}).then(
    //     res => {      
    //       console.log(res.resultData)     
    //       var obj = res.data.resultData;
    //         var arr = [];
    //         for(var i in obj) {
    //           var row = {};
    //           row.label = obj[i].pName;
    //           row.value = obj[i].id;
    //           arr.push(row);
    //         }
    //         //console.log(arr)
    //         self.groupOptions = arr;
    //     }
    //   );
    // },
    getGroupListOnly() {
      var self = this;
      //console.log(self.currentPage)
      self.getRequest("/base/common/queryProjectName?flug="+1, {}).then(
        res => {      
          console.log(res.resultData)     
          var obj = res.data.resultData;
            var arr = [];
            for(var i in obj) {
              var row = {};
              row.label = obj[i].pName;
              row.value = obj[i].id;
              arr.push(row);
            }
            //console.log(arr)
            self.groupOptionsOnly = arr;
        }
      );
    },
    deleteRow(index, rows) {
      console.log(rows[index])
      var self = this;
      self.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          self.
            postRequest("/weekly/deleteWeekly", {
              id:rows[index].id

            }).then(
              res => {      
                console.log(res.data) 
                if(res.data.result == '1' ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                }else if(res.data.result == '2' ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }    
                self.getTableList();
              }
            );
        }).catch(() => {
          self.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //修改
    handleClick(row) {
      console.log(row);
      var self = this;

      self.postRequest("/daily/compareUserIdAndLoginUser", {
          user_id:row.user_id
        }).then(
          res => {      
            console.log(res)     
            if(res.data.result == '1' ){            
              //console.log(self.codeBtype)
              self.codeBtype = false;
              self.getGroupListOnly();
              self.rowId = row.id;
              self.dialogFormVisible = true;
              self.form.group = row.group_id;
              self.form.weekly_date = row.weekly_date;
              self.form.summary_of_last_week = row.summary_of_last_week;
              self.form.this_week_plan = row.this_week_plan;
            }else if(res.data.result == '2' ){
              this.$message({
                  message: res.data.message,
                  type: 'error'
              });
            }
            
          }
        );
      
      
    },
    //查看
    handleViewClick(row) {
      var self = this;
      self.rowId = row.id;
      self.dialogFormVisibleView = true;
      self.viewGroup = row.group_name;
      self.viewDate = row.weekly_date;
      self.viewLast = row.summary_of_last_week;
      self.viewPlan = row.this_week_plan;
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    formSubmit(formName) {
      //console.log('submit!');      
      this.$refs[formName].validate((valid) => {
          if (valid) {
            var self = this;            
            self.postRequest("/weekly/updateWeekly", {
              id:self.rowId,
              group_id:self.form.group,
              weekly_date:self.form.weekly_date,
              summary_of_last_week:self.form.summary_of_last_week,
              this_week_plan:self.form.this_week_plan
            }).then(
              res => {      
                console.log(res)     
                if(res.data.result == '1' ){            
                  this.$message({
                      message: res.data.message,
                      type: 'success'
                  });
                }else if(res.data.result == '2' ){
                  this.$message({
                      message: res.data.message,
                      type: 'error'
                  });
                }
                self.dialogFormVisible = false;
                self.getTableList();
              }
            );
          }
        })
      
      
    },
    rootSelect() {
      var self = this;            
        self.getRequest("/currCrudPermissions", {

        }).then(
          res => {      
            //console.log(typeof res.data)     
            var arr = [];
            for (var i in res.data){
              arr.push(res.data[i]);
            }
            console.log(arr)
            if (arr.indexOf('修改')>-1){
              self.showModify = true;
            }else{
              self.showModify = false;
            }
            if (arr.indexOf('删除')>-1){
              self.showDel = true;
            }else{
              self.showDel = false;
            }
            if (arr.indexOf('查看')>-1){
              self.showLook = true;
            }else{
              self.showLook = false;
            }
            
          }
        );
    }
  }
}
</script>

<style scoped>
  .demo-form-inline {text-align: left;}
  .el-pagination {margin: 20px 0;}
  .text-left {text-align: left;}
  .mb0 {margin-bottom: 0;}
</style>